<template>
  <div class="front-main-content">
    <Card class="content-main station-main">
      <Table
        highlight-row
        :loading="false"
        :columns="columns"
        :data="data"
      >
        <template slot-scope="{ row }" slot="name">
          <span>{{ row.name }}</span>
        </template>
      </Table>
    </Card>
  </div>
</template>

<script>
// import moment from 'moment'
// import echarts from 'echarts'
import { getEquipmentlist, queryPileStatusList } from '@/api/data2'

export default {
  name: 'gunDetail',
  props: {
    pileId: {
      type: [String, Number]
    }
  },
  data () {
    return {
      loading: false,
      columns: [
        {
          title: this.$t('dUE9V6jUHBdS6m9avfiqp'),
          // key: 'name',
          slot: 'name',
          fixed: 'left',
          width: 150
        },
        {
          title: this.$t('U8ZxEg6llVjAqWal8YnPQ'),
          key: 'number',
          fixed: 'left',
          width: 150
        },
        {
          title: this.$t('E7tYaPxmVwFDI6XmyCafE'),
          key: 'type',
          width: 150
        },
        {
          title: this.$t('Ck_RpU1Yh0W_y7SIep2kT'),
          key: 'isfault',
          width: 150,
          render (h, params) {
            return h('span', params.row.isfault == 1 ? '是' : '否')
          }
        },
        {
          title: this.$t('Uyjjji1I7cH58N09tmadl'),
          key: 'workstate',
          width: 150,
          render: (h, params) => {
            const state = params.row.workstate
            let msg = this.$t('1konnpi4CshbQ85aHmAoW')
            if (state == 0) {
              msg = this.$t('0P9RF1EXCuEtRDWiziChz')
            } else if (state == 1) {
              msg = this.$t('sX1uwC-7i6LBOo7BqLUcd')
            } else if (state == 2) {
              msg = this.$t('RVCTbUrscq9OLbP_wxaoO')
            } else if (state == 3) {
              msg = this.$t('cofkDVyr3ZdMpFywSCCil')
            } else if (state == 4) {
              msg = this.$t('dUnYbIMLZSu4QP6-axK6a')
            } else if (state == 5) {
              msg = this.$t('H6TqtmVtvfmNnX-DPr-9R')
            }
            return h('span', msg)
          }
        },
        {
          title: this.$t('LNz9TBU3e5f2DmsLPm_vA'),
          key: 'soc',
          width: 150
        },
        {
          title: this.$t('XjvMMrdrO8SalYFohPLR3') + '(V)',
          key: 'voltage',
          width: 150
        },
        {
          title: this.$t('z-_Yz86SttcMn3A1orPbS') + '(A)',
          key: 'current',
          width: 150
        },
        {
          title: this.$t('mu2hviJYo3oZ9nhvkHjum') + '(kWh)',
          key: 'meterreading',
          width: 150
        },
        {
          title: this.$t('sgO8XTrpDs0hVu2JOZPmp') + '(kWh)',
          key: 'electric',
          width: 150
        },
        {
          title: this.$t('DyFHLriFxsvqhbeY8xUPW') + '(' + this.$t('rEwoS7D6EkR6EPGHSq6MU') + ')',
          key: 'money',
          width: 150
        },
        {
          title: this.$t('zIUBwj57pPWz4KE9aeaZH') + '(' + this.$t('I4guWEhjHUQJA5FgWQzKM') + ')',
          key: 'leavetime',
          width: 150
        },
        {
          title: this.$t('fANrdlSjcKKCJpUTfEi50') + '(kW)',
          key: 'outp',
          width: 150
        },
        {
          title: this.$t('KX0vdZ0kJD-tz0ZoNU99D'),
          key: 'devicetime',
          width: 150
        },
        {
          title: this.$t('043TML2CBGM-qVZDmDHeo'),
          key: 'systemtime',
          width: 150
        }
      ],
      data: [],
      timer: null
    }
  },
  mounted () {
    this.start()
  },
  watch: {
    async '$route' () {
      !_.isNil(this.timer) && clearInterval(this.timer)
      this.start()
    }
  },
  methods: {
    async start () {
      await this.queryInfo()
      this.timer = setInterval(() => {
        this.queryGunPolling()
      }, this.$config.intervalTime * 1000)
    },
    sleep (time) {
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve()
        }, time || this.$config.intervalTime * 1000)
      })
    },
    async queryInfo () {
      this.loading = true
      const { code, data, desc } = await getEquipmentlist({
        stationid: this.$store.state.user.stationId + ''
      })
      if (code == 200) {
        const equipInfo = this._origin = data.datalist || []
        if (equipInfo.length) {
          const equip = equipInfo.find(e => e.id == this.pileId)
          if (!equip) {
            this.$Message.error(this.$t('bZ8BU0UTQEgrGJ0_J9kQm'))
            return
          }
          const { code, data, desc } = await queryPileStatusList({
            devicelist: [String(this.pileId)]
          })
          if (code == 200) {
            const datalist = data.datalist || []
            if (datalist.length) {
              datalist[0].connectorlist.forEach(d => {
                d.name = equip.name
              })
              this.data = datalist[0].connectorlist
            } else {
              this.data = []
            }
          } else {
            this.$Message.error(desc)
          }
        }
      } else {
        this.$Message.error(desc)
      }
      this.loading = false
      // await this.sleep(this.$config.intervalTime * 1000)
      // this.queryInfo()
    },
    async queryGunPolling () {
      this.loading = true
      const equipInfo = _.cloneDeep(this._origin || [])
      if (equipInfo.length) {
        const equip = equipInfo.find(e => e.id == this.pileId)
        if (!equip) {
          this.$Message.error(this.$t('bZ8BU0UTQEgrGJ0_J9kQm'))
          return
        }
        const { code, data, desc } = await queryPileStatusList({
          devicelist: [String(this.pileId)]
        })
        if (code == 200) {
          const datalist = data.datalist || []
          if (datalist.length) {
            datalist[0].connectorlist.forEach(d => {
              d.name = equip.name
            })
            this.data = datalist[0].connectorlist
          } else {
            this.data = []
          }
        } else {
          this.$Message.error(desc)
        }
      } else {
        this.$Message.error(this.$t('vCi1wOiBGPckI3rzo0RJt'))
      }
      this.loading = false
      // await this.sleep()
      // this.queryGunPolling()
    }
  },
  beforeDestroy () {
    !_.isNil(this.timer) && clearInterval(this.timer)
  }
}
</script>
